package com.system.dao;

import com.am.base.BaseDao;
import com.system.uitls.CommonUtil;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class PriceDao extends BaseDao
{
  public List<Map<String, Object>> getPriceListNow(String userId, String campus)
  {
    String sql = "SELECT person_name,person_id,id,status FROM sys_patient_record WHERE (status=3 OR (status=4 AND user_id=?)) AND person_campus=?";
    return getSimpleJdbc().queryForList(sql, new Object[] { userId, campus });
  }

  public Map<String, Object> getWaitForPricePerson(String record_id, String user_id, String user_name)
  {
    Map result = null;
    String sql = null;
    try {
      sql = "SELECT status FROM sys_patient_record WHERE id=?";
      result = getSimpleJdbc().queryForMap(sql, new Object[] { record_id });

      if ("4".equals(result.get("status")))
      {
        result.put("repeat", "true");
        return result;
      }

      sql = "UPDATE sys_patient_record SET status=?,user_id=?,user_name=? WHERE id=?";
      getSimpleJdbc().update(sql, new Object[] { "4", user_id, user_name, record_id });
    }
    catch (Exception ex)
    {
      logger.info("PriceDao[getWaitForPricePerson] sql:" + sql + "\n" + ex.getMessage());
    }
    return result;
  }

  public Map<String, String> back(String record_id, int status)
  {
    Map result = new HashMap();
    String sql = null;
    try {
      sql = "UPDATE sys_patient_record SET status=? WHERE id=?";
      getSimpleJdbc().update(sql, new Object[] { Integer.valueOf(status), record_id });
      result.put("type", "success");
      result.put("value", "返回成功");
    }
    catch (Exception ex) {
      logger.info("PriceDao[back] sql:" + sql + "\n" + ex.getMessage());
      result.put("type", "fail");
      result.put("value", "返回失败");
    }
    return result;
  }

  public Map<String, String> priceSubmit(Map<String, Object> map)
  {
    Map result = new HashMap();
    String record_id = CommonUtil.nullToEmptyString(map.get("record_id"));

    String dressing_amount = CommonUtil.nullToEmptyString(map.get("dressing_amount"));
    String infuse_amount = CommonUtil.nullToEmptyString(map.get("infuse_amount"));
    String laboratory_amount = CommonUtil.nullToEmptyString(map.get("laboratory_amount"));
    double privatePay = Float.parseFloat(CommonUtil.nullToEmptyString(map.get("privatePay")));
    double publicPay = Float.parseFloat(CommonUtil.nullToEmptyString(map.get("publicPay")));
    int percentage = CommonUtil.convertToInt(CommonUtil.nullToEmptyString(map.get("percentage")), 100);

    double drug_amount_fact = Math.round((privatePay + publicPay * percentage / 100.0D) * 100.0D) / 100.0D;

    double drug_amount = privatePay + publicPay;

    String receivable_total = CommonUtil.nullToEmptyString(map.get("receivable_total"));

    String fact_total = CommonUtil.nullToEmptyString(map.get("fact_total"));

    String sql = null;
    try {
      sql = "update sys_patient_record set status=?,dressing_amount=?,infuse_amount=?,laboratory_amount=?,drug_amount_fact=?,drug_amount=?,percentage=?,receivable_total=?,amount=? WHERE id=?";
      getSimpleJdbc().update(sql, new Object[] { "5", dressing_amount, infuse_amount, laboratory_amount, Double.valueOf(drug_amount_fact), Double.valueOf(drug_amount), Integer.valueOf(percentage), receivable_total, fact_total, record_id });
      result.put("type", "success");
      result.put("value", "提交成功");
    }
    catch (Exception ex) {
      logger.info("DoctorDao[submit] sql:" + sql + "\n" + ex.getMessage());
      result.put("type", "fail");
      result.put("value", "提交失败");
    }
    return result;
  }

  public Map<String, Object> getPriceInfo(String record_id, String person_id)
  {
    Map result = null;
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(1);
    try
    {
      result = getSimpleJdbc().queryForMap("SELECT name,sex,t1.type,telephone,class,address,id_card,is_overdue,(SELECT percentage FROM sys_cost_definition t WHERE person_type=t1.type AND t.register_type=t2.register_type) as percentage FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND t2.id=?", new Object[] { record_id });

      String is_overdue = CommonUtil.nullToEmptyString(result.get("is_overdue"));

      if ("1".equals(is_overdue))
      {
        result.put("percentage", "100");
      }
      result.putAll(getSimpleJdbc().queryForMap("SELECT count(1) as times,isnull(sum(amount),0) as amount FROM sys_person t1,sys_patient_record t2 WHERE t1.id=t2.person_id AND DATEPART(yyyy,create_time)=? AND t2.person_id=? AND t2.status='7'", new Object[] { Integer.valueOf(year), person_id }));

      result.putAll(getSimpleJdbc().queryForMap("SELECT isnull(sum(t1.amount),0) as publicPay FROM sys_prescription t1,sys_drug t2,sys_diagnose t3 WHERE t1.drug_id=t2.id AND t2.is_own='1' AND t3.record_id=? AND t1.diagnose_id=t3.id", new Object[] { record_id }));

      result.putAll(getSimpleJdbc().queryForMap("SELECT isnull(sum(t1.amount),0) as privatePay FROM sys_prescription t1,sys_drug t2,sys_diagnose t3 WHERE t1.drug_id=t2.id AND t2.is_own='0' AND t3.record_id=? AND t1.diagnose_id=t3.id", new Object[] { record_id }));
      result.put("currentDate", CommonUtil.getFormatCurDate("yyyy年MM月dd日"));
    } catch (Exception ex) {
      result = new HashMap();
      logger.error("PriceDao[getPriceInfo]-->查询划价信息出错\n" + ex.getMessage());
    }

    return result;
  }
}